<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="题目"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
        @blur="handleFilter"
      />
      <el-select
        v-model="listQuery.type"
        placeholder="类别"
        style="width:200px"
        class="filter-item"
        size="mini"
        clearable
        @change="handleFilter"
      >
        <el-option label="选择题" :value="1">选择题</el-option>
        <el-option label="操作题" :value="2">操作题</el-option>
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        size="mini"
        style="margin-left:10px;"
        @click="handleFilter"
      >查询</el-button>
      <el-dialog :title="operation?'新增':'编辑'" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="问题" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="类型" :label-width="formLabelWidth">
            <el-radio v-model="form.type" label="1">选择题</el-radio>
            <el-radio v-model="form.type" label="2">操作题</el-radio>
          </el-form-item>
          <el-form-item v-show="form.type==2" label="问题资源" :label-width="formLabelWidth">
            <el-select v-model="form.resourceId" placeholder="请选择资源">
              <el-option v-for="(resource,i) in resourceList" :key="i" :label="resource.name" :value="resource.id" />
            </el-select>
          </el-form-item>
          <div v-show="form.type==1">
            <el-form-item label="选项A" :label-width="formLabelWidth">
              <el-input v-model="form.chooseA" autocomplete="off" />
            </el-form-item>
            <el-form-item label="选项B" :label-width="formLabelWidth">
              <el-input v-model="form.chooseB" autocomplete="off" />
            </el-form-item>
            <el-form-item label="选项C" :label-width="formLabelWidth">
              <el-input v-model="form.chooseC" autocomplete="off" />
            </el-form-item>
            <el-form-item label="选项D" :label-width="formLabelWidth">
              <el-input v-model="form.chooseD" autocomplete="off" />
            </el-form-item>
            <el-form-item label="答案" :label-width="formLabelWidth">
              <el-radio v-model="form.answer" label="A">A</el-radio>
              <el-radio v-model="form.answer" label="B">B</el-radio>
              <el-radio v-model="form.answer" label="C">C</el-radio>
              <el-radio v-model="form.answer" label="D">D</el-radio>
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addProblem">确 定</el-button>
        </div>
      </el-dialog>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        size="mini"
        style="margin-left:10px;"
        @click="newProblem"
      >新增</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoadding"
      :data="list"
      size="mini"
      border
      fit
      highlight-current-row
      style="width:100%"
      @sort-change="sortChange"
    >
      <el-table-column
        label="序号"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="{ row: { id }}">
          <span>{{ id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="题目"
        align="center"
        width="280"
      >
        <template slot-scope="{ row: { pname }}">
          <span>{{ pname }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="类型"
        align="center"
        width="80"
      >
        <template slot-scope="{ row: { ptype }}">
          <el-tag :type="ptype==1?'':'success'">{{ ptype==1?'选择题':'操作题' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="资源"
        align="center"
        width="180"
      >
        <template slot-scope="{ row: { medialUrl }}">
          <span>{{ medialUrl==null ?'无':medialUrl }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="选项A"
        align="center"
        width="120"
      >
        <template slot-scope="{ row: { chooseA }}">
          <span>{{ chooseA }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="选项B"
        align="center"
        width="120"
      >
        <template slot-scope="{ row: { chooseB }}">
          <span>{{ chooseB }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="选项C"
        align="center"
        width="120"
      >
        <template slot-scope="{ row: { chooseC }}">
          <span>{{ chooseC }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="选项D"
        align="center"
        width="120"
      >
        <template slot-scope="{ row: { chooseD }}">
          <span>{{ chooseD }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="答案"
        align="center"
        width="80"
      >
        <template slot-scope="{ row: { answer }}">
          <span>{{ answer }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        width="80"
      >
        <template slot-scope="{ row: { createBy }}">
          <span>{{ createBy }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        width="80"
      >
        <template slot-scope="{ row: { createTime }}">
          <span>{{ createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="195"
      >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editProblem(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="deleteData(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>=0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="handlePageList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'
import { getProblemList, updateProblem, deleteProblem } from '@/api/problem'
import { getResourceList } from '../../api/teachplan'
export default {
  inject: ['reload'],
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    const generateData = _ => {
      const data = []
      for (let i = 1; i <= 15; i++) {
        data.push({
          key: i,
          label: `学号 ${i}`,
          disabled: i % 4 === 0
        })
      }
      return data
    }
    return {
      tableKey: 0,
      listLoadding: true,
      list: [],
      total: 0,
      listQuery: {
      },
      operation: true,
      form: {
        id: '',
        name: '',
        resourceId: '',
        medialUrl: '',
        type: '',
        chooseA: '',
        chooseB: '',
        chooseC: '',
        chooseD: '',
        answer: ''
      },
      formLabelWidth: '120px',
      dialogFormVisible: false,
      data: generateData(),
      value: [1],
      value4: [1],
      resourceList: [],
      rowData: {
      },
      renderFunc(h, option) {
        return <span>{ option.key } - { option.label }</span>
      }
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
  },
  methods: {
    parseQuery() {
      // 解析查询条件
      const liatQuery = {
        page: 1,
        pageSize: 10
      }
      // 合并查询条件
      this.listQuery = { ...liatQuery, ...this.listQuery }
    },
    getList() {
      this.listLoadding = true
      // 调用接口
      getProblemList(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.content
        this.total = response.data.totalSize
      })
      this.listLoadding = false
    },
    handleFilter() {
      this.getList()
    },
    // 点击不同页时调用的方法
    handlePageList() {
      this.list = this.getList()
    },
    sortChange(data) {
      console.log('排序' + data)
    },
    handleChange(value, direction, movedKeys) {
      console.log(value, direction, movedKeys)
    },
    addProblem() {
      // 创建问题
      const form = Object.assign({}, this.form)
      updateProblem(form).then(response => {
        this.$message({
          type: 'success',
          message: this.operation ? '新增成功' : '编辑成功'
        })
        this.reload()
        this.dialogFormVisible = false
      }).catch(response => {
        this.$message({
          type: 'error',
          message: this.operation ? '新增失败' : '编辑失败'
        })
      })
    },
    editProblem(row) {
      console.log(row)
      getResourceList().then(response => {
        console.log(response.data)
        this.resourceList = response.data
      })
      this.form.name = row.pname
      this.form.type = row.ptype
      this.form.chooseA = row.chooseA
      this.form.chooseB = row.chooseB
      this.form.chooseC = row.chooseA
      this.form.chooseD = row.chooseD
      this.form.answer = row.answer
      this.form.id = row.id
      this.form.resourceId = row.resourceId
      this.form.medialUrl = row.medialUrl
      this.dialogFormVisible = true
      this.operation = false
    },
    newProblem() {
      this.dialogFormVisible = true
      getResourceList().then(response => {
        console.log(response.data)
        this.resourceList = response.data
      })
      this.form = {
        id: '',
        name: '',
        resourceId: '',
        medialUrl: '',
        type: '',
        chooseA: '',
        chooseB: '',
        chooseC: '',
        chooseD: '',
        answer: ''
      }
    },
    deleteData(row) {
      this.$confirm('此操作将永久删除该问题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用接口
        deleteProblem(row.id).then(response => {
          this.reload()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>
.transfer-footer {
    margin-left: 20px;
    padding: 6px 5px;
  }
</style>
